'use strict';

const gulp = require('gulp');
const browserSync = require('browser-sync');
const nodemon = require('gulp-nodemon');
const less = require('gulp-less');
const path = require('path');
const watch = require('gulp-watch');

gulp.task('default', ['browser-sync'], function () {
});

gulp.task('browser-sync', ['nodemon','less'], function() {
    browserSync.init(null, {
        proxy: "http://localhost:3000", // 服务端口一般是3000
        files: ["public/**/*.*"],
        browser: "google chrome",
        port: 7000,
    });
    gulp.watch(path.join(__dirname,'less/**/*.less'), ['less']);
    gulp.watch(path.join(__dirname,'views/**/*.art')).on('change', browserSync.reload);
});
gulp.task('less',function() {
    return gulp.src(path.join(__dirname,'less/**/*.less'))
        .pipe(less())
        .pipe(gulp.dest('./public/styles'))
        .pipe(browserSync.stream())
})
gulp.task('nodemon', function (cb) {
    var started = false;
    
    return nodemon({
        script: 'app.js'
    }).on('start', function () {
        // to avoid nodemon being started multiple times
        // thanks @matthisk
        if (!started) {
            cb();
            started = true; 
        } 
    });
});